//
//  BA_HomeNaviView.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/6.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BA_HomeNaviView.h"
#import "GYRollingNoticeView.h"

@interface BA_HomeNaviView ()<GYRollingNoticeViewDataSource, GYRollingNoticeViewDelegate>

@property (nonatomic, strong) GYRollingNoticeView *noticeView;

@end

@implementation BA_HomeNaviView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.homeCityControl.userInteractionEnabled = YES;
    self.homeSearchControl.userInteractionEnabled = YES;
    [self.homeCityControl addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if (self.BA_HomeNaviViewBlock) {
            self.BA_HomeNaviViewBlock(HomeNaviActionType_changeCity, -1);
        }
    }];
    
    [self.homeSearchControl addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {

        if (self.BA_HomeNaviViewBlock) {
            self.BA_HomeNaviViewBlock(HomeNaviActionType_search, -1);
        }
    }];
    
    [self.noticeScrollView addSubview:self.noticeView];
    [self.noticeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
    [RACObserve([TT_MessageManager manager], totalUnReadCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].totalUnReadCount > 0) {
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.msgBtn.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].totalUnReadCount];
                self.msgBtn.badgeBGColor = [UIColor redColor];
            });

        } else {
            self.msgBtn.badgeValue = @"";
            self.msgBtn.badgeBGColor = [UIColor clearColor];
        }
    }];
    
    [[TT_MessageManager manager] refreshUnReadCount];
    
}

- (void)fillWithModel:(HomeHeadModel *)model {
    
    self.model = model;
    
    if (model.advBackImg.length > 0) {
        [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:model.advBackImg]];
    } else {
        [self.bgImageView setImage:[UIImage imageNamed:@"BAmarriage1"]];
    }
    
    [self.noticeView reloadDataAndStartRoll];
}

- (NSInteger)numberOfRowsForRollingNoticeView:(GYRollingNoticeView *)rollingView {
    return self.model.sysAdvEventList.count;
}


- (__kindof GYNoticeViewCell *)rollingNoticeView:(GYRollingNoticeView *)rollingView cellAtIndex:(NSUInteger)index {
    
    GYNoticeViewCell *cell = [rollingView dequeueReusableCellWithIdentifier:@"GYNoticeViewCell"];
    cell.textLabel.text = self.model.sysAdvEventList[index].iconName;
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.textLabel.textColor = UIColorHex(0x666666);
    cell.backgroundColor =UIColor.clearColor;
    cell.contentView.backgroundColor = UIColor.clearColor;
    return cell;
}

//轮播的点击事件
- (void)didClickRollingNoticeView:(GYRollingNoticeView *)rollingView forIndex:(NSUInteger)index {
    if (self.BA_HomeNaviViewBlock) {
        self.BA_HomeNaviViewBlock(HomeNaviActionType_search, index);
    }
}

- (void)fillWithAlpha:(CGFloat)alpha {
    CGFloat a = 1 - alpha;
    self.bgImageView.alpha = a;
    if (alpha > 0.5) {
        self.homeSearchControl.backgroundColor = UIColorHex(0xf5f5f5);
    } else {
        self.homeSearchControl.backgroundColor = UIColorHex(0xf5f5f5);
    }
}

- (IBAction)goMsgAction:(id)sender {
    if (self.BA_HomeNaviViewBlock) {
        self.BA_HomeNaviViewBlock(HomeNaviActionType_message, -1);
    }
}

- (IBAction)goQRCodeAction:(id)sender {
    if (self.BA_HomeNaviViewBlock) {
        self.BA_HomeNaviViewBlock(HomeNaviActionType_qrCode, -1);
    }
}

- (GYRollingNoticeView *)noticeView {
    
    if (!_noticeView) {
        _noticeView = [[GYRollingNoticeView alloc]init];
        _noticeView.dataSource = self;
        _noticeView.delegate = self;
        _noticeView.stayInterval = 3;
        _noticeView.backgroundColor = UIColor.clearColor;
        [_noticeView registerClass:[GYNoticeViewCell class] forCellReuseIdentifier:@"GYNoticeViewCell"];
    }
    return _noticeView;
}

@end
